package com.ruiben.mall.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlUtils {

    public static String removeServerUrlImgUrl(String content, String replaceStr){
        Document mDocument = Jsoup.parse(content);
        Elements imageSrc = mDocument.select("img[src]");

        for (int i=0;i<imageSrc.size();i++){
            Element element = imageSrc.get(i);
            if (element.attr("src").startsWith(replaceStr)){
                String src = element.attr("src").substring(replaceStr.length(),element.attr("src").length());
                element.attr("src",src);
            }
        }
        return mDocument.body().html();
    }

    public static String addServerUrlImgUrl(String content, String replaceStr){
        Document mDocument = Jsoup.parse(content);
        Elements imageSrc = mDocument.select("img[src]");

        for (int i=0;i<imageSrc.size();i++){
            Element element = imageSrc.get(i);
            if (!element.attr("src").startsWith("http")){
                element.attr("src",replaceStr+element.attr("src"));
            }
        }
        return mDocument.body().html();
    }
}
